package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Requisition;
import com.zyd.shiro.business.service.PakRequisitionService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakRequisition;
import com.zyd.shiro.persistence.mapper.PakRequisitionMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakRequisitionServiceImpl implements PakRequisitionService {

    @Autowired
    private PakRequisitionMapper pakRequisitionMapper;

    @Override
    public Requisition insert(Requisition requisition) {
        Assert.notNull(requisition, "对象不可为空！");
        requisition.getPakRequisition().setUnicode("SL"+System.currentTimeMillis());
        requisition.getPakRequisition().setAppstatus(0);
        requisition.getPakRequisition().setUpdateTime(new Date());
        requisition.getPakRequisition().setCreateTime(new Date());
        pakRequisitionMapper.insertSelective(requisition.getPakRequisition());
        return requisition;
    }

    @Override
    public void insertList(List<Requisition> requisitions) {
            for(Requisition requisition:requisitions){
               requisition.getPakRequisition().setUpdateTime(new Date());
               requisition.getPakRequisition().setCreateTime(new Date());
               pakRequisitionMapper.insertSelective(requisition.getPakRequisition());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakRequisitionMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Requisition requisition) {
        Assert.notNull(requisition, "对象不可为空！");
        requisition.setUpdateTime(new Date());
        return pakRequisitionMapper.updateByPrimaryKey(requisition.getPakRequisition()) > 0;
    }

    @Override
    public boolean updateSelective(Requisition requisition) {
        Assert.notNull(requisition, "对象不可为空！");
        requisition.setUpdateTime(new Date());
        return pakRequisitionMapper.updateByPrimaryKeySelective(requisition.getPakRequisition()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Requisition getByPrimaryKey(Long primaryKey) {
        PakRequisition pakRequisition = pakRequisitionMapper.selectByPrimaryKey(primaryKey);
        return null == pakRequisition ? null : new Requisition(pakRequisition);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Requisition getOneByEntity(Requisition test) {
        Assert.notNull(test, "对象不可为空！");
        PakRequisition pakRequisition = pakRequisitionMapper.selectOne(test.getPakRequisition());
        return null == pakRequisition ? null : new Requisition(pakRequisition);
    }

    @Override
    public List<Requisition> listAll() {
        List<PakRequisition> pakRequisitions = pakRequisitionMapper.selectAll();
        if (CollectionUtils.isEmpty(pakRequisitions)) {
            return null;
        }
        List<Requisition> requisitions = new ArrayList<>();
        for (PakRequisition pakRequisition : pakRequisitions) {
            requisitions.add(new Requisition(pakRequisition));
        }
        return requisitions;
    }

    @Override
    public List<Requisition> listByEntity(Requisition entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Requisition> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakRequisition> pakRequisitions = pakRequisitionMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakRequisitions)) {
            return null;
        }
        List<Requisition> requisitions = new ArrayList<>();
        for (PakRequisition pakRequisition : pakRequisitions) {
            requisitions.add(new Requisition(pakRequisition));
        }
        PageInfo bean = new PageInfo<PakRequisition>(pakRequisitions);
        bean.setList(requisitions);
        return bean;
    }
@Override
    public void increase(PakRequisition pakRequisition) {
        pakRequisitionMapper.insert(pakRequisition);
    }




}
